<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: NewsPortlet.php 407 2011-01-04 21:20:31Z steen.rabol $
 * @package Pradoportal.Porttlets.NewsPortlet.Edit
 *
 */

error_reporting(E_ALL);
Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Common.Data.PortalConfigRecord');
Prado::using('Application.Portlets.NewsPortlet.Common.NewsPortletRecord');

class NewsPortlet extends Portlet
{
	private $_newsconfig	= null;

	public function  getConfigName()
	{
		return 'News';
	}

	public function getNewsConfig()
	{
		return $this->_newsconfig;
	}

	public function setNewsConfig($value)
	{
		$this->_newsconfig	= $value;
	}

	public function UserName($id)
	{
		return $this->User->Name;
	}

	public function dataBind()
	{
		$this->Showheaders->Checked		= TPropertyValue::ensureBoolean($this->NewsConfig['showheaders']);
		$this->ShowCount->Text			= $this->NewsConfig['showcount'];
		$this->NewsDataList->DataSource	= NewsPortletRecord::finder()->findAllBycontentid($this->Parameters['id']);
		$this->NewsDataList->dataBind();
	}

	public function onInit($param)
	{
		parent::onInit($param);

		if($this->_newsconfig === null)
		{
			$crow = $this->ConfigRecord;

			if($crow)
			{
				$this->_newsconfig = @unserialize($crow->value);
				$this->_newsconfig['id'] = $crow->id;
			}
			else
			{
				$this->_newsconfig	= array("showheaders" => 1, "showcount" => 5);
				$value		= serialize($this->_newsconfig);
				$rec		= new PortalConfigRecord();
				$rec->value	= $value;
				$rec->name	= 'News_' . $this->Parameters['id'];
				$rec->save();

				$this->_newsconfig['id'] = $rec->id;
			}
		}
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		
		if(!$this->Page->isPostBack)
			$this->dataBind();
	}

	public function AddNewsBtnClicked($sender, $param)
	{
		if($this->Page->IsValid)
		{
			$rec	 = new NewsPortletRecord();

			$rec->contentid		= $this->Parameters['id'];
			$rec->subject		= $this->Subject->Text;
			$rec->body			= $this->Body->Text;
			$rec->expire		= $this->Expire->TimeStamp;
			$rec->created		= time();
			$rec->createdby		= $this->User->Id;
			$rec->modified		= $rec->created;
			$rec->modifiedby	= $this->User->Id;
			$rec->culture		= $this->Application->Culture;
			$rec->save();
			$this->dataBind();
		}
	}

	public function selectItem($sender,$param)
	{
		$this->NewsDataList->EditItemIndex=-1;
		$this->dataBind();
	}

	public function editItem($sender,$param)
	{
		$this->NewsDataList->SelectedItemIndex=-1;
		$this->NewsDataList->EditItemIndex=$param->Item->ItemIndex;
		$this->dataBind();
	}

	public function cancelItem($sender,$param)
	{
		$this->NewsDataList->SelectedItemIndex=-1;
		$this->NewsDataList->EditItemIndex=-1;
		$this->dataBind();
	}

	public function getCulture()
	{
		return $this->Application->Culture;
	}

	public function updateItem($sender,$param)
	{
		$item				= $param->Item;
		$id					= $this->NewsDataList->DataKeys[$item->ItemIndex];
		$rec	 			= NewsPortletRecord::finder()->findByid($id);
		$rec->subject		= $item->edtSubject->Text;
		$rec->body			= $item->edtBody->Text;
		$rec->expire		= $item->edtExpire->TimeStamp;
		$rec->modified		= time();
		$rec->modifiedby	= $this->User->Id;
		$rec->save();

		$this->NewsDataList->EditItemIndex=-1;
		$this->dataBind();
	}

	public function SaveNewsOptionsBtnClicked($sender, $param)
	{
		$value		= serialize(array("showheaders" => TPropertyValue::ensureInteger($this->Showheaders->Checked), "showcount" => TPropertyValue::ensureInteger($this->ShowCount->Text)));
		$rec		= PortalConfigRecord::finder()->findByid($this->NewsConfig['id']);
		$rec->value	= $value;
		$rec->save();
	}

	public function deleteItem($sender,$param)
	{
		$id = $this->NewsDataList->DataKeys[$param->Item->ItemIndex];
		NewsPortletRecord::finder()->deleteByid($id);
		$this->NewsDataList->SelectedItemIndex	= -1;
		$this->NewsDataList->EditItemIndex		= -1;
		$this->dataBind();
	}

	public function DeleteButtonClicked($sender,$param)
	{
		PortalConfigRecord::finder()->deleteByname('News_' . $this->Parameters['id']);
		parent::DeleteButtonClicked($sender,$param);
	}
}
?>